<template>
  <div class="login-wrap">
    <el-form label-position="left" :model="loginForm" :rules="rules" ref="loginForm" label-width="0px"
             class="demo-loginForm login-container">
      <h3 class="title">FlyCat登录</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="15">
          <el-form-item prop="code">
            <el-input type="text" v-model="loginForm.code" auto-complete="off" placeholder="图形验证码"
                      @keyup.enter.native="submitForm('loginForm')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="9" class="code-box">
          <img :src="captchaImg" alt="" class="codeimg" @click="getcaptchaImg()">
        </el-col>
      </el-row>
      <!--      <el-checkbox class="remember" v-model="rememberpwd">记住密码</el-checkbox>-->
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="submitForm('loginForm')">登录</el-button>
        <!--        <el-button type="primary" style="width:100%;" @click="submitForm('loginForm')" :loading="logining">登录</el-button>-->
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import qs from "qs";
import {mapActions} from "vuex";

export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        code: "",
        token: "",
      },
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
          {
            min: 0,
            max: 11,
            message: "长度在 3 到 11 个字符",
            trigger: "blur",
          },
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {
            min: 0,
            max: 11,
            message: "长度在 3 到 11 个字符",
            trigger: "blur",
          },
        ],
        code: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      captchaImg: null
    };
  },
  methods: {
    ...mapActions(["catUser/setUser", "setShowLogin", "getUser"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("/login?" + qs.stringify(this.loginForm))
            .then((res) => {
              console.log("login");
              this.$message({
                showClose: false,
                message: "登陆成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  // 登录信息存到本地 用户名 "用户名"
                  let user = JSON.stringify(res.data.data.user);
                  // let user = res.data.data.user
                  localStorage.setItem("user", user);
                  // 登录信息存到vuex
                  localStorage.setItem("user", user);
                  this.$store.commit('catUser/setUser', user)
                  console.log("this.$store.getters.getUser", this.$store.getters["catUser/getUser"])
                  // this.setUser(res.data.data.user);

                  // 把token 放到 store
                  // const jwt = res.headers["authorization"];
                  // console.log("在下面")
                  // console.log(jwt)
                  // console.log(res)
                  // this.$store.commit("SET_TOKEN", jwt);
                  this.$router.push("/");
                },
              });
            });
        } else {
          this.getcaptchaImg()
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getcaptchaImg() {
      this.$axios.get("/captcha").then((res) => {
        // console.log(res.data.data.captchaImg);

        this.loginForm.token = res.data.data.token;
        this.captchaImg = res.data.data.captchaImg;
      });
    },
  },
  mounted() {
    console.log("create");
    this.getcaptchaImg();
  },
};
</script>

<style scoped>
.login-wrap {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  /*padding-top: 0%;*/
  margin-top: 50px;
  margin-bottom: 60px;
  background-image: url(https://raw.githubusercontent.com/xzMhehe/StaticFile_CDN/main/static/mo/1.png);
  /* background-color: #112346; */
  background-repeat: no-repeat;
  background-position: center right;
  background-size: 100%;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}

.remember {
  margin: 0px 0px 35px 0px;
}

.code-box {
  text-align: right;
}

.codeimg {
  height: 40px;
  width: 95px;
}
</style>
